package com.evolved.automata.experimental;




public class SimpleRobotUtilityInterpreter implements UtilityAndPerferenceModule {
	
	int j_MinimumDistance;
	
	int j_PreviousDistance;
	
	UtilityListener j_Listener;
	String j_AgentName;
	
	public SimpleRobotUtilityInterpreter(UtilityListener listener, String agentName, int minimumDistance, int initialPreviousDistance)
	{
		j_Listener=listener;
		j_MinimumDistance=minimumDistance;
		j_PreviousDistance=initialPreviousDistance;
		j_AgentName=agentName;
	}
	
	public SimpleRobotUtilityInterpreter()
	{
		j_MinimumDistance=0;
		j_PreviousDistance=0;
	}
	
	
	public void SetMinimumDistance(int mindist)
	{
		j_MinimumDistance=mindist;
	}
	
	public void SetInitialPreviousDist(int iniPrevDist)
	{
		j_PreviousDistance=iniPrevDist;
	}
	
	private Utility GetNextUtility(int[] currentState, int[] actionsChosen)
	{
		int currentDistance=0;
		if (currentState==null)
			currentDistance=j_PreviousDistance;
		else
			currentDistance=currentState[0];
		if (currentDistance<0)
			return UtilityAndPerferenceModule.Utility.NEUTRAL;
		
		if (currentDistance<j_MinimumDistance)
		{
			j_PreviousDistance=currentDistance;
			return UtilityAndPerferenceModule.Utility.NEGATIVE;
		}
		else if ((j_PreviousDistance<j_MinimumDistance)&&(currentDistance>=j_MinimumDistance))
		{
			j_PreviousDistance=currentDistance;
			return UtilityAndPerferenceModule.Utility.POSITIVE;
		}
		else
		{
			j_PreviousDistance=currentDistance;
			return UtilityAndPerferenceModule.Utility.NEUTRAL;
		}
	}
	
	public void SetUtilityListener(UtilityListener listener)
	{
		j_Listener=listener;
	}
	
	public Utility UpdateState(int[] currentState, int[] actionsChosen)
	{
		Utility nextUtility=GetNextUtility(currentState,actionsChosen);
		j_Listener.UpdateUtility(j_AgentName, nextUtility);
		
		return nextUtility;
	}

	public int GetUtilityType()
	{
		return UtilityAndPerferenceModule.KEEP_DISTANCE_GREATER_THAN_MINIMUM;
	}

	public void UpdateData(int[] parameters)
	{
		j_MinimumDistance=parameters[0];
		j_PreviousDistance=parameters[1];
	}
	
	public int[] GetGeneticParameters()
	{
		return new int[]{j_MinimumDistance,j_PreviousDistance};
	}
}
